package com.mall.manager.consult.service;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service
public class ManagerConsultService {
	private Logger logger = LoggerFactory.getLogger(ManagerConsultService.class);
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * @功能 	: 根据咨询id查询的操作
	 * @param 	: paraMap
	 * @return 	:
	 */
	public Map<String, Object> manager_consult_findbyid(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		String sql="select *,date_format(create_date,'%Y-%m-%d') create_date_new from db_consult.t_consult where id=?";
		try {
			Map<String, Object> queryForMap = jdbcTemplate.queryForMap(sql,id);
			logger.info("根据咨询id查询成功！");
			return queryForMap;
		} catch (Exception e) {
			logger.info("根据咨询id查询失败！");
		}
		return null;
	}
	
	
	/**
	 * @功能 	: 对咨询表的添加和修改操作
	 * @param 	: paraMap
	 * @return 	:
	 */
	public int manager_consult_add(Map<String, Object> paraMap) {
		String title = MapUtils.getString(paraMap, "title");
		String author = MapUtils.getString(paraMap, "author");
		String content = MapUtils.getString(paraMap, "content");
		int c_id = MapUtils.getIntValue(paraMap, "c_id");
		String route = MapUtils.getString(paraMap, "route");
		String publish_date = MapUtils.getString(paraMap, "publish_date");
		String create_date = MapUtils.getString(paraMap, "create_date");
		String update_date = MapUtils.getString(paraMap, "update_date");
		try {
			int id = MapUtils.getIntValue(paraMap, "id");
			int addNum=0;
			String sql = "";
			if(id==0){
				sql = "INSERT INTO db_consult.t_consult (title, author, content,c_id,route,publish_date,create_date, update_date) VALUES (?, ?, ?, ?, ?, ?, NOW(), ?)";
				addNum = this.jdbcTemplate.update(sql, title, author,content,c_id,route,publish_date,update_date);
				if (addNum > 0) {
					logger.info("咨询添加成功！");
				}
			}else{
				sql="update db_consult.t_consult set title=?, author=?, content=?,c_id=?,route=?,publish_date=?,create_date=?, update_date=NOW() where id=?";
				addNum= jdbcTemplate.update(sql,title, author,content,c_id,route,publish_date,create_date,id);
				if (addNum > 0) {
					logger.info("咨询修改成功！");
				}
			}
			return addNum;
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("咨询操作失败！【错误】:manager_consult_add方法中！");
			return 0;
		}
	}
	
	
}
